import 'package:base/main.dart';
import 'package:flutter/material.dart';
import 'package:framework/main.dart';
import 'package:module_discover/pages/filter_domain/filter_domain_logic.dart';
import 'package:module_discover/pages/model/Domain_info_model.dart';
import 'package:module_discover/utils/dis_cons.dart';
import 'package:module_discover/widgets/animation_dialog_widget.dart';
import 'package:module_discover/widgets/elevated_button_widget.dart';

class DomainBottomBarWidget extends StatelessWidget {
  final FilterDomainLogic logic;
  final VoidCallback? onPressed;

  const DomainBottomBarWidget({super.key, required this.logic, this.onPressed});

  @override
  Widget build(BuildContext context) {
    return Obx(() {
      if (logic.filterPreviewVM.selectedMaps.isEmpty) {
        return SizedBox();
      }
      return Container(
        color: Colors.white,
        height: 100,
        padding: EdgeInsets.all(10),
        child: Column(
          children: [
            Row(
              children: [
                Obx(() => logic.filterPreviewVM.getSelectedChildSize() <= 0
                    ? Text("您还没有添加条件哦！",
                        style:
                            TextStyle(color: Colors.blueAccent, fontSize: 12))
                    : Text(
                        "已添加${logic.filterPreviewVM.getSelectedChildSize()}个条件",
                        style: TextStyle(color: Colors.red, fontSize: 12))),
                Expanded(child: SizedBox.fromSize()),
                InkWell(
                  child: Row(
                    children: [
                      Text(
                        "点击查看",
                        style: TextStyle(color: Colors.black, fontSize: 12),
                      ),
                      Icon(
                        Icons.arrow_forward_ios,
                        color: Colors.grey,
                        size: 16,
                      )
                    ],
                  ),
                  onTap: () {
                    AnimationDialogWidget.showDialog(context,
                        childWidget: _buildDialog(context));
                  },
                ),
              ],
            ),
            Row(
              children: [
                Expanded(
                    child: ElevatedButtonWidget(
                  onPressed: () {
                    Log.i("重置条件");
                    logic.resetAll();
                  },
                  title: "重置条件",
                  backgroundColor: JColor.color_f3f3f3,
                  textColor: Colors.black87,
                )),
                SizedBox(width: 16),
                Expanded(
                    child: ElevatedButtonWidget(
                  onPressed: onPressed,
                  title: "立即筛选",
                  backgroundColor: Colors.red,
                  textColor: Colors.white,
                ))
              ],
            )
          ],
        ),
      );
    });
  }

  _buildDialog(BuildContext context) {
    return Obx(() => SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Row(
                children: [
                  SizedBox(width: 10),
                  Text(
                      '已添加的条件(${logic.filterPreviewVM.getSelectChildLists().length})',
                      style: TextStyle(
                        fontSize: 12,
                        decoration: TextDecoration.none,
                        color: Colors.black,
                      )),
                  Expanded(child: SizedBox()),
                  Material(
                    color: Colors.transparent, // 设置背景透明
                    child: InkWell(
                      onTap: () {
                        Navigator.of(context).pop(); // 关闭对话框
                      },
                      child: Icon(
                        Icons.dangerous,
                        color: Colors.grey,
                        size: 18,
                      ),
                    ),
                  ),
                  SizedBox(width: 10),
                ],
              ),
              Padding(
                padding: const EdgeInsets.only(top: 10),
                child: Wrap(
                  alignment: WrapAlignment.start,
                  spacing: 8.0, // 子组件之间的水平间距
                  runSpacing: 8.0, // 行与行之间的垂直间距
                  children:
                      logic.filterPreviewVM.getSelectChildLists().map((info) {
                    return _buildInfoItem(info);
                  }).toList(),
                ),
              )
            ],
          ),
        ));
  }

  Widget _buildInfoItem(DomainInfoModel info) {
    var values = info.key;
    if (info.value.contains(DisCons.SPLIT_S)) {
      values = info.value;
    }
    if (values == null) {
      values == "";
    }
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 8),
      padding: EdgeInsets.symmetric(horizontal: 8.0, vertical: 4.0),
      decoration: BoxDecoration(
        color: JColor.color_f3f3f3,
        borderRadius: BorderRadius.circular(4.0),
      ),
      child: Material(
        color: Colors.transparent,
        child: InkWell(
          onTap: () {
            logic.deleteSelectedMap(info.parentKey, info.childKey);
            logic.selectInputVM.resetByKey(info.childKey);
          },
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              ConstrainedBox(
                constraints: BoxConstraints(maxWidth: 200),
                child: Text('${info.name}:$values',
                    overflow: TextOverflow.ellipsis,
                    maxLines: 1,
                    style: TextStyle(
                        fontSize: 10,
                        decoration: TextDecoration.none,
                        color: Colors.black)),
              ),
              SizedBox(width: 4),
              Icon(Icons.dangerous_outlined,
                  color: Color(0xFFA3B1C8), size: 12),
            ],
          ),
        ),
      ),
    );
  }
}
